#include "mainwindow.h"
#include "connexion.h"

#include <QApplication>
#include <QTextCodec>
#include <QFile>
#include <QDebug>

using namespace ConnexionSpace;

void myMessageOutput(QtMsgType type, const char *msg)
 {
     QFile file(qApp->applicationDirPath ()+"/errorLog.txt");
     file.open(QIODevice::ReadWrite|QIODevice::Append);
     QTextStream s(&file);
     switch (type) {
     case QtDebugMsg:
         s << "Debug: " << QString(msg) << "\n";
         break;
     case QtWarningMsg:
         s << "Warning: " << QString(msg) << "\n";
         break;
     case QtCriticalMsg:
         s << "Critical: " << QString(msg) << "\n";
         break;
     case QtFatalMsg:
         s << "Fatal: " << QString(msg) << "\n";
         abort();
     }
     file.close();
 }

int main (int argc, char * argv[])
{
    //qInstallMsgHandler(myMessageOutput);
    QApplication a(argc,argv);
    QFile f(a.applicationDirPath ()+"/errorLog.txt");
    f.remove();
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
    Connexion *c = new Connexion;
    if (!c->connectToDrTux())
    {
          qWarning() << __FILE__ << QString::number(__LINE__) << "unable to connect to Dr Tux" ;
        }
    QStringList args = qApp->arguments();
    QStringList arglist ;
    if (args[1] == "--program_name=test")
    {
          enum TestEnum {TEST_PROGRAM=0,TEST_TEST,TEST_NAME,TEST_FIRSNAME,TEST_DATEOFBIRTH,TEST_USER,TEST_GUID,TestEnumMaxParam};
          for ( int e = 0; e < TestEnumMaxParam; ++ e)
          {
                if (e!=TEST_TEST)
                {
                    arglist << args[e];
                    }
                qDebug() << __FILE__ << QString::number(__LINE__) << " arglist =" << args[e] ;
              }
        }
    enum DrTuxProgramEnum
    {
            PROGRAM_NAME=0,
            PATIENT_NAME,
            PATIENT_FIRSTNAME,
            PATIENT_DATEOFBIRTH,
            MEDINTUX_USER,
            PATIENT_GUID            
            };
    int countArgs = args.size();
    QString program_name,name,firstname,dateofbirth,guid,user;
    if(countArgs == 14)
    {
        QString GUID_Patient = qApp->arguments()[4];
        QString argsOfMedintux = qApp->arguments()[13];
        QStringList argsOfMedintuxList = argsOfMedintux.split(";");
        arglist.append( argsOfMedintuxList[PROGRAM_NAME]);
        arglist.append( argsOfMedintuxList[PATIENT_NAME]);
        arglist.append( argsOfMedintuxList[PATIENT_FIRSTNAME]);
        arglist.append( argsOfMedintuxList[PATIENT_DATEOFBIRTH]);
        arglist.append( argsOfMedintuxList[MEDINTUX_USER]);
        arglist.append( "--patientuuid="+GUID_Patient);
            }
    for ( int i = 0; i < arglist.size(); ++ i)
    {
          QStringList list;
          if (arglist[i].contains("="))
          {
              list = arglist[i].split("=");
              }
          else{
               list.append("program_name");
               list.append(arglist[i]);
               }
          switch(i){
              case PROGRAM_NAME :
                  program_name = list[1];
                  break;
              case PATIENT_NAME :
                  name = list[1];
                  break;
              case PATIENT_FIRSTNAME :
                  firstname = list[1];
                  break;
              case PATIENT_DATEOFBIRTH :
                  dateofbirth = list[1];
                  break;
              case MEDINTUX_USER :
                  user = list[1];
                  break;                  
              case PATIENT_GUID :
                  guid = list[1];
                  break;
              default :
                  break;    
              }              
        }
    qDebug() << __FILE__ << QString::number(__LINE__) << " main name =" << name << firstname << guid << user;
    MainWindowRequete * mw = new MainWindowRequete;
    mw->setInformations(name,firstname,guid,user);
    mw->runAnnualResearchWidget();
    mw->show();    
    return a.exec();
    delete mw;
}
